<?php
require_once( 'DatasourceDecorator.php' );

class XmlStreamDatasource extends DatasourceDecorator {

	function XmlStreamDatasource( $oXmlStream ) {
		
		if ( !is_resource( $oXmlStream ) ) {
			$this->oDatasource = new NullDatasource();
			$this->oDatasource->AddError( constant( 'ErrorNumber.DatasourceRetrieval' ), 'XML stream is not a resource' );
			return;
		}

		$sFile = FileHandler::ReadFromStream( $oXmlStream );

		if ( !$sFile ) {
			$this->oDatasource = new NullDatasource();
			$this->oDatasource->AddError( constant( 'ErrorNumber.DatasourceRetrieval' ), 'XML stream could not be read or did not contain any data' );
			return;
		}
		
		$oParser = new XmlParser();
		$oDatabaseXmlDoc = $oParser->Parse( $sFile );
		if ( !$oDatabaseXmlDoc ) {
			$this->oDatasource = new NullDatasource();
			$this->oDatasource->AddError( constant( 'ErrorNumber.DatasourceRetrieval' ), 'XML stream did not contain a valid XML file' );
			return;
		}

		$this->oDatasource = new XmlDatasource( $oDatabaseXmlDoc );
	}
}

require_once( 'XmlDatasource.php' );
require_once( 'NullDatasource.php' );

?>
